package cn.keniykey.mc.viewlibrary.view.api;

import java.util.HashMap;
import java.util.UUID;

import javax.annotation.Nullable;

import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;

import cn.keniykey.mc.viewlibrary.reflection.ReflectionConstructor;

public abstract class AbPluginViewContent {
	/*
	 * Date: 2024/2/24
	 * 
	 */
	private final Plugin plugin;
	private final Player player;
	volatile int view_amount;
	
	protected AbPluginViewContent(Plugin plugin1,Player player1) {
		this.plugin = plugin1;
		this.player = player1;
	}
	
	private static HashMap<String,ReflectionConstructor> map_plugins_content_constructor = new HashMap<>();
	private static HashMap<UUID,HashMap<String,AbPluginViewContent>> map_players_plugins_content = new HashMap<>();
	
	public static void register(Plugin plugin,Class<? extends AbPluginViewContent> clazz) {
		final ReflectionConstructor constructor;
		try {
			constructor = new ReflectionConstructor(clazz.getDeclaredConstructor(Player.class));
		}catch(Exception ex) {
			throw new RuntimeException("cannot find the constructor for only player data(找不到单独Player数据的构建方法): " + clazz.getName(), ex);
		}
		map_plugins_content_constructor.put(plugin.getName(), constructor);
	}
	public static void unRegister(Plugin plugin) {
		map_plugins_content_constructor.remove(plugin.getName());
	}
	@Nullable
	static AbPluginViewContent newObject(Player player,Plugin plugin) {
		final String plugin_name = plugin.getName();
		if(map_players_plugins_content.containsKey(player.getUniqueId())) {
			final HashMap<String, AbPluginViewContent> acmap = map_players_plugins_content.get(player.getUniqueId());
			if(acmap.containsKey(plugin_name)) return acmap.get(plugin_name);
		}
		if(map_plugins_content_constructor.containsKey(plugin_name)) {
			final AbPluginViewContent activity = (AbPluginViewContent) map_plugins_content_constructor.get(plugin_name).newInstance(player);
			final UUID player_uuid = player.getUniqueId();
			final HashMap<String, AbPluginViewContent> acmap;
			if(map_players_plugins_content.containsKey(player_uuid)) {
				acmap = map_players_plugins_content.get(player_uuid);		
			}else {
				acmap = new HashMap<>();
				map_players_plugins_content.put(player_uuid, acmap);
			}
			acmap.put(plugin_name, activity);
			return activity;
		}
		return null;
	}
	static void removeOfCache(UUID player_uuid,@Nullable Plugin plugin) {
		if(!map_players_plugins_content.containsKey(player_uuid)) return;
		final HashMap<String, AbPluginViewContent> acmap = map_players_plugins_content.get(player_uuid);
		if(null == plugin) {
			acmap.clear();
		}else {
			acmap.remove(plugin.getName());
		}
		if(acmap.isEmpty()) map_players_plugins_content.remove(player_uuid);
	}
	
	public final Plugin getPlugin() {
		return this.plugin;
	}
	public final Player getPlayer() {
		return this.player;
	}
	/**
	 * 当某位玩家第一次创建某插件所属的View时触发
	 */
	protected void onCreate() {}
	/**
	 * 当某View显示后触发
	 */
	protected void onInvoke(final AbFrameView view) {}
	/**
	 * 当某View不显示后触发
	 */
	protected void onPause(final AbFrameView view) {}
	/**
	 * 当玩家打开的所属View全部销毁，再无其它所属View时触发
	 */
	protected void onDestroy() {}
}